package cn.lcc.system.service.impl;

import cn.lcc.system.domain.Role;
import cn.lcc.system.mapper.RoleMapper;
import cn.lcc.system.service.IRoleService;
import cn.lcc.basic.service.impl.BaseServiceImpl;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void save(Role role) {
        super.save(role);
        if(role.getPermissions()!=null&&role.getPermissions().size()>0){
            roleMapper.addRolePermission(role);
        }
        if(role.getMenus()!=null&&role.getMenus().size()>0){
            roleMapper.addRoleMenu(role);
        }
    }

    @Override
    public void del(Long id) {
        //删除当前角色的所有权限
        roleMapper.delPermissionByRoleId(id);
        //删除当前角色的所有菜单
        roleMapper.delMenuByRoleId(id);
        super.del(id);
    }

    @Override
    public void update(Role role) {
        super.update(role);
        //删除当前角色的所有权限
        roleMapper.delPermissionByRoleId(role.getId());
        //删除当前角色的所有菜单
        roleMapper.delMenuByRoleId(role.getId());
        if(role.getPermissions()!=null&&role.getPermissions().size()>0){
            roleMapper.addRolePermission(role);
        }
        if(role.getMenus()!=null&&role.getMenus().size()>0){
            roleMapper.addRoleMenu(role);
        }
    }

    @Override
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            del(id);
        }
    }
}
